<?php

namespace PHPSpring\Annotation\Impl;

use PHPSpring\Core\ProxyFactory;
use PHPSpring\Core\RuntimeAnnotation;

class Service {

  public static function postInit($target, $daoClass=null) {
    $re = new \ReflectionClass($target);
    if (empty($daoClass)) {
      $annos = RuntimeAnnotation::getAnnotations(get_class($target));
      foreach ($annos['properties'] as $name => $anno) {
        // use the first dao instance
        if (preg_match('/dao$/i', $name)) {
          $daoClass = $anno['type'];
          break;
        }
      }
    }
    $property = $re->getProperty('baseDao');
    $property->setAccessible(true);
    $property->setValue($target, ProxyFactory::getProxy($daoClass));
  }
}
